package com.platform.enums;

import com.platform.comm.enums.IDictEntry;
import org.apache.commons.compress.utils.Lists;

import java.util.Arrays;
import java.util.List;

/**
 * 双公示枚举类
 *
 * @author zq
 */
public enum DataSgsEnum implements IDictEntry {
    /**
     * 双公示的资源信息,共四种
     */
    nat_xzxk_info("nat_xzxk_info", "自然人行政许可信息", "DIR-20210411-092448-934"),
    leg_xzxk_info("leg_xzxk_info", "法人行政许可信息", "DIR-20210411-091928-150"),
    nat_xzcf_info("nat_xzcf_info", "自然人行政处罚信息", "DIR-20210411-093705-257"),
    leg_xzcf_info("leg_xzcf_info", "法人行政处罚信息", "DIR-20210411-093350-43");

    public static final String XK = "xk";
    public static final String CF = "cf";
    /**
     * 表名
     */
    private final String tableName;
    /**
     * 资源名称
     */
    private final String resourceName;
    /**
     * 资源标识符
     */
    private final String resourceKey;

    DataSgsEnum(String tableName, String resourceName, String resourceKey) {
        this.tableName = tableName;
        this.resourceName = resourceName;
        this.resourceKey = resourceKey;
    }

    /**
     * 根据枚举"键"值解析枚举值
     *
     * @param tableName 资源编码
     * @return DataSgsEnum
     */
    public static DataSgsEnum parse(String tableName) {
        for (DataSgsEnum item : values()) {
            if (item.is(tableName)) {
                return item;
            }
        }
        throw new RuntimeException("资源编码不规范");
    }

    public boolean is(String resourceBm) {
        return this.tableName.equals(resourceBm);
    }

    @Override
    public String getKey() {
        return name();
    }

    public String getCode() {
        return this.tableName;
    }

    @Override
    public String getValue() {
        return this.resourceName;
    }

    @Override
    public Object getData() {
        return this;
    }

    public String getResourceKey() {
        return this.resourceKey;
    }

    public String getResourceName() {
        return this.resourceName;
    }

    public static List<String> getResourceKeys() {
        List<String> resourceKeys = Lists.newArrayList();
        for (DataSgsEnum item : values()) {
            resourceKeys.add(item.getResourceKey());
        }
        return resourceKeys;
    }

    public static List<String> getTableNames() {
        List<String> tableNames = Lists.newArrayList();
        for (DataSgsEnum item : values()) {
            tableNames.add(item.getCode());
        }
        return tableNames;
    }
    public static List<String> getXzcfTableNames() {
        List<String> tableNames = Lists.newArrayList();
        tableNames.add("nat_xzcf_info");
        tableNames.add("leg_xzcf_info");
        return tableNames;
    }
    public static String getNameByTable(String table) {
        return Arrays.stream(values()).filter(i -> i.tableName.equals(table))
                .map(i -> i.getResourceName()).findFirst().orElse(null);
    }


    public boolean isEnum(String resourceKey) {
        return this.resourceKey.equals(resourceKey);
    }

    /**
     * 根据枚举"键"值解析枚举值
     *
     * @param resourceKey 资源标识符
     * @return DataSgsEnum
     */
    public static DataSgsEnum parseByResouceKey(String resourceKey) {
        for (DataSgsEnum item : values()) {
            if (item.isEnum(resourceKey)) {
                return item;
            }
        }
        throw new RuntimeException("资源标识符不规范");
    }
}
